package org.asiainfo.flowable.controller;

import lombok.RequiredArgsConstructor;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import org.asiainfo.flowable.domain.bo.WfTaskBo;
import org.asiainfo.flowable.service.WfInstanceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 流程实例管理
 *
 * @author dotor-ww
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wfInstance")
@Validated
public class WfInstanceController extends BaseController {

    private final WfInstanceService wfInstanceService;

    /**
     * 激活/挂起流程实例
     *
     * @param instanceId   流程实例ID
     * @param suspendState 1-激活，2-挂起
     * @return 激活/挂起结果
     */
    @PostMapping("/updateState")
    public R<Void> updateState(@RequestParam String instanceId, @RequestParam Integer suspendState) {
        wfInstanceService.updateState(instanceId, suspendState);
        return R.ok();
    }

    /**
     * 结束流程实例
     *
     * @param wfTaskBo 流程任务业务对象
     * @return 结束结果
     */
    @PostMapping("/stopProcessInstance")
    public R<Void> stopProcessInstance(@RequestBody WfTaskBo wfTaskBo) {
        wfInstanceService.stopProcessInstance(wfTaskBo);
        return R.ok();
    }

    /**
     * 删除流程实例
     *
     * @param instanceId 流程实例ID
     * @param reason     删除原因
     * @return 删除结果
     */
    @DeleteMapping("/deleteProcessInstance")
    public R<Void> deleteProcessInstance(@RequestParam String instanceId, @RequestParam String reason) {
        wfInstanceService.deleteProcessInstance(instanceId, reason);
        return R.ok();
    }

    /**
     * 流程实例详情
     *
     * @param procInsId 流程实例ID
     * @param deployId  流程部署ID
     * @return 流程实例详情
     */
    @GetMapping("/detail")
    public R<Object> detail(String procInsId, String deployId) {
        return R.ok(wfInstanceService.queryProcessDetail(procInsId, deployId));
    }
}
